#include<graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#pragma comment(lib,"winmm.lib")
IMAGE beijing, bird, tc, wall1, wall2, tc1, tc2;
int b_x;
int b_y;
int wallup_x, wallup_y;

void gameover();
void start()
{
	mciSendString(_T("open D:\\Desktop\\flappy birdbackground.mp3 alias bkmusic"), NULL, 0, NULL);
	mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);
	initgraph(350, 600);
	loadimage(&beijing, _T("D:\\Desktop\\flappy bird\\background.jpg"));
	loadimage(&tc, _T("D:\\Desktop\\flappy bird\\bird1.jpg"));
	loadimage(&bird, _T("D:\\Desktop\\flappy bird\\bird2.jpg"));
	loadimage(&wall1, _T("D:\\Desktop\\flappy bird\\bar_up2.gif"));
	loadimage(&tc1, _T("D:\\Desktop\\flappy bird\\bar_up1.gif"));
	loadimage(&wall2, _T("D:\\Desktop\\flappy bird\\bar_down2.gif"));
	loadimage(&tc2, _T("D:\\Desktop\\flappy bird\\bar_down1.gif"));
	//BeginBatchDraw();
	b_x = 34;
	b_y = 24 + 175;
	wallup_x = 210;
	wallup_y = -300;

}

void show()
{
	putimage(0, 0, &beijing);
	putimage(b_x, b_y, &tc, NOTSRCERASE);
	putimage(b_x, b_y, &bird, SRCINVERT);
	putimage(wallup_x, wallup_y, &tc1, NOTSRCERASE);
	putimage(wallup_x, wallup_y, &wall1, SRCINVERT);
	putimage(wallup_x, wallup_y + 100 + 600, &tc2, NOTSRCERASE);
	putimage(wallup_x, wallup_y + 100 + 600, &wall2, SRCINVERT);
	FlushBatchDraw();
}

void input()
{
	char c;
	if (kbhit())
	{
		c = getch();
		if (c = ' ')
		{
			b_y -= 30;
		}
	}
}

void outinput()
{
	wallup_x -= 10;
	b_y += 10;
	Sleep(150);
	if (wallup_x <= -140)
	{
		wallup_x = 350;
		wallup_y = (-((rand() % 300) + 220));
	}
	if (wallup_x <= 68 && wallup_x + 140 >= 68)
	{
		if (b_y <= wallup_y + 600 || b_y + 24 >= wallup_y + 600 + 100)
		{
			gameover();
		}
	}
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
	exit(0);
}

int main()
{
	start();
	while (1)
	{
		show();
		input();
		outinput();

	}
	getch();
	closegraph();
	return 0;
}